/*
	创建角色程序
*/

package net.sf.odinms.net.login.handler;

import net.sf.odinms.client.Equip;
import net.sf.odinms.client.Item;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleCharacterUtil;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.MapleInventory;
import net.sf.odinms.client.MapleInventoryType;
import net.sf.odinms.client.MapleSkinColor;
import net.sf.odinms.net.AbstractMaplePacketHandler;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

public class CreateCharHandler extends AbstractMaplePacketHandler {

    private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CreateCharHandler.class);

    @Override
    public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
        //11 00 [08 00 B9 FE B9 FE D9 E2 D2 B6] [03 00 00 00] [00 00] [84 4E 00 00] [30 75 00 00] [F7 E6 0F 00] [E3 34 10 00] [FF 5C 10 00] [1F 01 16 00]  //088的创建职业封包,貌似是send的
        //11 00 [08 00 CA B2 C3 B4 CA B2 B4 F3] [00 00 00 00] [00 00] [B1 4F 00 00] [30 75 00 00] [3E 06 10 00] [00 00 00 00] [A6 5B 10 00] [15 2C 14 00]//089
        String name = slea.readMapleAsciiString();//角色名
        int job = slea.readInt(); //职业
        slea.readShort();
        int face = slea.readInt(); //脸型
        int hair = slea.readInt(); //发型
        int hairColor = 0;        //发色
        int skinColor = 0;        //肤色
        int top = slea.readInt(); //上衣
        int bottom = slea.readInt(); //裙裤
        int shoes = slea.readInt(); //鞋子
        int weapon = slea.readInt(); //武器
        int gender = c.getGender(); //性别

        MapleCharacter newchar = MapleCharacter.getDefault(c);
        if (c.isGM()) {
            newchar.setGM(1);
        }
        newchar.setWorld(c.getWorld());
        newchar.setFace(face);//生成脸型
        newchar.setHair(hair + hairColor);//生成头发和发色
        newchar.setGender(gender);

        if(job == 0){ //反抗者 089建立没问题的，什么装备都没啊
            newchar.setStr(13);
            newchar.setDex(4);
            newchar.setInt(4);
            newchar.setLuk(4);
            newchar.setRemainingAp(0);
            newchar.setJob(3000);
            //送新手指导书
            newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161054, (byte) 0, (short) 1));
        }else if(job == 1){ //冒险家  089建立没问题的，什么装备都没啊
            newchar.setStr(13);
            newchar.setDex(4);
            newchar.setInt(4);
            newchar.setLuk(4);
            newchar.setRemainingAp(0);
            newchar.setJob(0);
            newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161001, (byte) 0, (short) 1));
       }else if(job == 2){//骑士团  089建立没问题的，什么装备都没啊
            newchar.setStr(11);
            newchar.setDex(6);
            newchar.setInt(4);
            newchar.setLuk(4);
            newchar.setRemainingAp(0);
            newchar.setJob(1000);
            //送新手指导书
            newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161047, (byte) 0, (short) 1));
       }else if(job == 3){//战神 
            newchar.setStr(11);
            newchar.setDex(6);
            newchar.setInt(4);
            newchar.setLuk(4);
            newchar.setRemainingAp(0);
            newchar.setJob(2000);
            //送新手指导书
            newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161048, (byte) 0, (short) 1));
     }
        else if(job == 4){ // 龙神  089建立没问题的，什么装备都没啊
            newchar.setStr(6);
            newchar.setDex(4);
            newchar.setInt(11);
            newchar.setLuk(4);
            newchar.setRemainingAp(0);
            newchar.setJob(2001);
            //送新手指导书
            newchar.getInventory(MapleInventoryType.ETC).addItem(new Item(4161052, (byte) 0, (short) 1));
     }
        newchar.setName(name);//生成角色名
        newchar.setSkinColor(MapleSkinColor.getById(skinColor));//生成皮肤颜色

        MapleInventory equip = newchar.getInventory(MapleInventoryType.EQUIPPED);
        Equip eq_top = new Equip(top, (byte) -5);//生成上衣
        eq_top.setWdef((short) 3);
        eq_top.setUpgradeSlots((byte) 7);
        equip.addFromDB(eq_top.copy());
        if(bottom!= 0){
          Equip eq_bottom = new Equip(bottom, (byte) -6);//生成裙裤
          eq_bottom.setWdef((short) 2);
          eq_bottom.setUpgradeSlots((byte) 7);
          equip.addFromDB(eq_bottom.copy());
        }
        Equip eq_shoes = new Equip(shoes, (byte) -7);//生成鞋子
        eq_shoes.setWdef((short) 2); //rite? o_O
        eq_shoes.setUpgradeSlots((byte) 7);
        equip.addFromDB(eq_shoes.copy());
        Equip eq_weapon = new Equip(weapon, (byte) -11);//生成武器
        eq_weapon.setWatk((short) 15);
        eq_weapon.setUpgradeSlots((byte) 7);
        equip.addFromDB(eq_weapon.copy());
        if (MapleCharacterUtil.canCreateChar(name, c.getWorld())) { //检测角色名
            newchar.saveToDB(false);
            c.getSession().write(MaplePacketCreator.addNewCharEntry(newchar, true));
        } else {
            log.warn(MapleClient.getLogMessage(c, "Trying to create a character with a name: {}", name));
        }
    }
}
